package com.nf.health.app.activity.home.view;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.nf.health.app.R;
import com.nf.health.app.activity.home.bean.SatisfactionSurveyQuestionBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by he_pan on 2017/4/12.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author he_pan
 * @Description
 */

public class MultipleSelectView extends BaseSelectView {

    protected final int MAX_SELECT_NUMBER = 9;//最大选择数

    protected TextView tvQuestionTitle;
    protected LinearLayout llContent;
    protected CheckBox cbAnswer1;
    protected CheckBox cbAnswer2;
    protected CheckBox cbAnswer3;
    protected CheckBox cbAnswer4;
    protected CheckBox cbAnswer5;
    protected CheckBox cbAnswer6;
    protected CheckBox cbAnswer7;
    protected CheckBox cbAnswer8;
    protected CheckBox cbAnswer9;
    protected LinearLayout llOne;
    protected LinearLayout llTwo;
    protected LinearLayout llThree;

    protected List<CheckBox> checkBoxList;//保存 checkbox 的引用

    public MultipleSelectView(Context context, SatisfactionSurveyQuestionBean bean, int position) {
        super(context, bean, position);
        initView();
        setUpView();
    }

    protected void initView() {
        View v = inflater.from(context).inflate(R.layout.item_survey_question_mult, this);
        tvQuestionTitle = (TextView) v.findViewById(R.id.tv_question_title);
        llContent = (LinearLayout) v.findViewById(R.id.ll_content);
        cbAnswer1 = (CheckBox) findViewById(R.id.cb_answer1);
        cbAnswer2 = (CheckBox) findViewById(R.id.cb_answer2);
        cbAnswer3 = (CheckBox) findViewById(R.id.cb_answer3);
        cbAnswer4 = (CheckBox) findViewById(R.id.cb_answer4);
        cbAnswer5 = (CheckBox) findViewById(R.id.cb_answer5);
        cbAnswer6 = (CheckBox) findViewById(R.id.cb_answer6);
        cbAnswer7 = (CheckBox) findViewById(R.id.cb_answer7);
        cbAnswer8 = (CheckBox) findViewById(R.id.cb_answer8);
        cbAnswer9 = (CheckBox) findViewById(R.id.cb_answer9);
        llOne = (LinearLayout) findViewById(R.id.ll_one);
        llTwo = (LinearLayout) findViewById(R.id.ll_two);
        llThree = (LinearLayout) findViewById(R.id.ll_three);
        checkBoxList = new ArrayList<>();
        checkBoxList.add(cbAnswer1);
        checkBoxList.add(cbAnswer2);
        checkBoxList.add(cbAnswer3);
        checkBoxList.add(cbAnswer4);
        checkBoxList.add(cbAnswer5);
        checkBoxList.add(cbAnswer6);
        checkBoxList.add(cbAnswer7);
        checkBoxList.add(cbAnswer8);
        checkBoxList.add(cbAnswer9);
    }

    protected void setUpView() {
        tvQuestionTitle.setText(position + 1 + ". " + bean.getTitle());
        List<String> optionList = bean.getOptionList();
        int optionSize = optionList.size();
        //设置ll的显示,默认都是gone,防止产生多余空白
        if (optionSize > 0 && optionSize < 3) {
            llOne.setVisibility(VISIBLE);
        } else if (optionSize < 6) {
            llOne.setVisibility(VISIBLE);
            llTwo.setVisibility(VISIBLE);
        } else {
            llOne.setVisibility(VISIBLE);
            llTwo.setVisibility(VISIBLE);
            llThree.setVisibility(VISIBLE);
        }
        //最多支持九个多选项
        if (optionSize > MAX_SELECT_NUMBER) {
            for (int i = 0; i < MAX_SELECT_NUMBER; i++) {
                checkBoxList.get(i).setVisibility(VISIBLE);
                checkBoxList.get(i).setText(optionList.get(i));
            }
        } else {
            //显示需要的,隐藏多余的
            for (int i = 0; i < MAX_SELECT_NUMBER; i++) {
                if (i < optionSize) {
                    checkBoxList.get(i).setVisibility(VISIBLE);
                    checkBoxList.get(i).setText(optionList.get(i));
                } else {
                    checkBoxList.get(i).setVisibility(GONE);
                }
            }
        }
    }

    public String getPosition() {
        return bean.getQnumber();
    }

    public String getSurveyAnswer() {
        StringBuilder answer = new StringBuilder();
        CheckBox cb;
        for (int i = 0; i < MAX_SELECT_NUMBER; i++) {
            cb = checkBoxList.get(i);
            if (cb.getVisibility() == VISIBLE && cb.isChecked()) {
                answer.append(cb.getText().toString().trim() + ",");
            }
        }
        String result = answer.toString();
        if (result.endsWith(",")) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }
}
